﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using System.Text.RegularExpressions;

namespace HExcelToXML
{
    class XPaymentExcelToXML : XExcelToXml
    {
        private Excel.Application m_xlsApplication;
        private Excel.Workbook m_xlsWorkbook;
        Excel.Worksheet m_xlsWorksheet;
        Excel.Range m_xlsRange;

        private int m_rows, m_columns;
        /* ********************************
         *  Constructor
         * ********************************/
        public XPaymentExcelToXML()
        {
            m_xlsApplication = new Excel.Application();
        }

        public EErrorCode parseRange()
        {
            System.String startCell = "A1";
            System.String endCell = "A" + m_rows;
            Excel.Range currencyRange = m_xlsRange.get_Range(startCell,endCell);

            foreach (object element in currencyRange.Value2)
            {
                if (null != element)
                {
                    if (element.GetType() == typeof(string))
                    {
                        Match matchCurrency = Regex.Match((string)element, @"^Currency.*$", RegexOptions.IgnoreCase);
                        if (matchCurrency.Success)
                        {
                            string[] words = ((string)element).Split(':');
                            words[1] = words[1].Replace(" ", "");
                            words[1] = words[1].Trim();
                            //todo: Currency node
                        }
                    }
                }
            }
            
            return 0;
        }


        /* ********************************
         * override Function
         * ********************************/
        public override EErrorCode parseExcel()
        {
            // Open Excel File
            object misValue = System.Reflection.Missing.Value;

            try
            {
                System.String xlsPath = base.getExcelFile();
                if (xlsPath == null)
                    return EErrorCode.k_file_name_null;

                m_xlsWorkbook = m_xlsApplication.Workbooks.Open(
                    xlsPath, misValue, true, misValue, "", "", misValue,
                    Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
                    "\t", false, false, 0, misValue, misValue, false);
                m_xlsWorksheet= m_xlsWorkbook.Worksheets.get_Item(1); //the first worksheet
                m_xlsRange = m_xlsWorksheet.UsedRange;

                m_rows = m_xlsRange.Rows.Count;
                m_columns = m_xlsRange.Columns.Count;

                if (m_rows == 0 || m_columns == 0)
                    return EErrorCode.k_invalid_data_format;
                
                parseRange();
            }
            catch (Exception ex)
            {
                return EErrorCode.k_exception;
            }
            return 0;
        }

 
    }
}
